import webview
import threading
import http.server
import socketserver
from dirlist import get_directory_list
import json
import os

PORT = 9999

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/dirlist.py':
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()

            json_string = json.dumps(get_directory_list())
            self.wfile.write(bytes(json_string, 'utf-8'))
        else:
            super().do_GET()

def start_server():
    with socketserver.TCPServer(("", PORT), MyHttpRequestHandler) as httpd:
        print("serving at port", PORT)
        httpd.serve_forever()

def open_new_window(window):
    # 创建并显示一个新窗口，加载指定的 URL
    new_window = window.open('New Window', 'https://www.example.com', width=600, height=400)
    # 也可以加载HTML内容
    # new_window = window.open('New Window', html=html_content, width=600, height=400)

if __name__ == '__main__':
    t = threading.Thread(target=start_server)
    t.start()

    main_window=webview.create_window("Directory Viewer", "http://localhost:9999/static/index.html", resizable=True, text_select=True)

    #调试模式下有开发者工具
    #webview.start(debug=True)
    webview.start()